//
//  FTPopOverMenuView+DLPop.m
//  Runtime_DemoOne
//
//  Created by meitianhui2 on 2017/12/26.
//  Copyright © 2017年 DeLongYang. All rights reserved.
/*
    问题一：
    1.0 单例的sharedInstance 方法没有重写  ？？？
    2.0 运行时替换的方法 是否会造成 循环引用 ？？？
 */

#import "FTPopOverMenuView+DLPop.h"
#import <objc/runtime.h>  // 引入 runtime
#import "YYKit.h"
#import "SecondViewController.h"    // 第二个VC里面 我们改变 选中的item的颜色

const NSString *currentSelectedKey = @"selectedIndex";
const NSString *reOpenSelectdedKey = @"reOpenSelected";
const NSString *selectedColorKey = @"selectedColor";
const NSString *textAlignmentKey = @"textAlignment";


@implementation FTPopOverMenuView (DLPop)


/**
 在load 方法中 做方法替换
 */
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 这个动态替换的方法来自于 YYKit  替换掉需要执行的方法
        [self swizzleInstanceMethod:@selector(tableView:cellForRowAtIndexPath:) with:@selector(dl_tableView:cellForRowAtIndexPath:)];
//        [self swizzleInstanceMethod:@selector(tableView:didSelectRowAtIndexPath:) with:@selector(dl_tableView:didSelectRowAtIndexPath:)];
    });
}


- (UITableViewCell *)dl_tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorColor = [UIColor redColor];  // set the color with red
    FTPopOverMenuCell *cell = (FTPopOverMenuCell *)[self dl_tableView:tableView cellForRowAtIndexPath:indexPath];
    UILabel *lable = [cell valueForKey:@"menuNameLabel"];  // 这里使用KVC 获取 Lable
    NSIndexPath *index = [FTPopOverMenu sharedInstance].currentSelected;
    BOOL canSeleted =  [FTPopOverMenu sharedInstance].reOpenSelected;
    if (indexPath.section == index.section && indexPath.row == index.row) {
        UIColor *color =  [FTPopOverMenu sharedInstance].selectedColor;
        if (color) {
            lable.textColor = color;
        }else{
            lable.textColor = [UIColor orangeColor];
        }
    }
    //
    NSTextAlignment textAlignment = [FTPopOverMenu sharedInstance].textAlignment;
    if (textAlignment != NSNotFound) {
        lable.textAlignment = textAlignment;
    }
    //
    return cell;
}


//- (void)dl_tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    if (self.doneBlock) {
//        self.doneBlock(indexPath.row);
//    }
//    
//    
//}


@end


@implementation FTPopOverMenu (DLPop)

- (void)setCurrentSelected:(NSIndexPath *)currentSelected
{
    [self setAssociateValue:currentSelected withKey:(__bridge void * _Nonnull)(currentSelectedKey)];
    if (currentSelected.row != NSNotFound || currentSelected != nil ) {
        self.reOpenSelected  = YES;
    }else{
        self.reOpenSelected = NO;
    }
}

- (NSIndexPath *)currentSelected
{
    return [self getAssociatedValueForKey:(__bridge void * _Nonnull)(currentSelectedKey)];
}

- (void)setReOpenSelected:(BOOL)reOpenSelected
{
    [self setAssociateValue:@(reOpenSelected) withKey:(__bridge void * _Nonnull)(reOpenSelectdedKey)];
}

- (BOOL)reOpenSelected
{
    NSNumber *num = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(reOpenSelectdedKey)];
    return  num.boolValue;
}

- (void)setSelectedColor:(UIColor *)selectedColor
{
    [self setAssociateValue:selectedColor withKey:(__bridge void * _Nonnull)(selectedColorKey)];
}

- (UIColor *)selectedColor
{
    UIColor *color = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(selectedColorKey)];
    return color;
}

- (void)setTextAlignment:(NSTextAlignment )textAlignment
{
    [self setAssociateValue:@(textAlignment) withKey:(__bridge void * _Nonnull)(textAlignmentKey)];
}

- (NSTextAlignment)textAlignment
{
    NSNumber *textAlignment = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(textAlignmentKey)];
    if (textAlignment != nil) {
        return NSNotFound;
    }
    //
    return textAlignment.integerValue;
}

+ (void)setFTPopOverViewWidthMenuWidth:(CGFloat)menuWidth rowHeight:(CGFloat)rowHeight textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment
{
    [FTPopOverMenuConfiguration defaultConfiguration].menuRowHeight = rowHeight;
    [FTPopOverMenuConfiguration defaultConfiguration].menuWidth = menuWidth;
    [FTPopOverMenuConfiguration defaultConfiguration].textColor = textColor;
    [FTPopOverMenuConfiguration defaultConfiguration].tintColor = [UIColor whiteColor];  // 自己改的设置成白色
    [FTPopOverMenuConfiguration defaultConfiguration].borderColor = [UIColor lightGrayColor];  // 设置border 的颜色 为灰色
    [FTPopOverMenuConfiguration defaultConfiguration].borderWidth = 0.5;
    [FTPopOverMenuConfiguration defaultConfiguration].textAlignment = textAlignment;
    [FTPopOverMenuConfiguration defaultConfiguration].textFont = [UIFont fontWithName:@"Heiti SC" size:14];  // 设置特定的 字体
    
}


@end

@implementation UIViewController (DLPop)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleInstanceMethod:@selector(viewDidAppear:) with:@selector(dlpop_viewDidAppear:)];
        [self swizzleInstanceMethod:@selector(viewWillDisappear:) with:@selector(dlpop_viewWillDisappear:)];  //
    });
}

- (void)dlpop_viewDidAppear:(BOOL)animated
{
    if ([self isKindOfClass:[SecondViewController class]])
    {
        NSNumber *number = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(reOpenSelectdedKey)];
        [FTPopOverMenu sharedInstance].reOpenSelected = number.boolValue;
        NSIndexPath *indexPath = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(currentSelectedKey)];
        [FTPopOverMenu  sharedInstance].currentSelected = indexPath;
        UIColor *color = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(selectedColorKey)];
        [FTPopOverMenu sharedInstance].selectedColor = color;
        NSNumber *textAlignment = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(textAlignmentKey)];
        if (textAlignment) {
            [FTPopOverMenu sharedInstance].textAlignment = textAlignment.intValue;
        }
        
    }
    [self dlpop_viewDidAppear:animated];
}


- (void)dlpop_viewWillDisappear:(BOOL )animated
{
    if ([self isKindOfClass:[SecondViewController class]])
    {
        FTPopOverMenu *menu = [FTPopOverMenu sharedInstance];
        [self setAssociateValue:@(menu.reOpenSelected) withKey:(__bridge void * _Nonnull)(reOpenSelectdedKey)];
        [self setAssociateValue:menu.currentSelected withKey:(__bridge void * _Nonnull)(currentSelectedKey)];
        [self setAssociateValue:@(menu.textAlignment) withKey:(__bridge void * _Nonnull)(textAlignmentKey)];
        [self setAssociateValue:menu.selectedColor withKey:(__bridge void * _Nonnull)(selectedColorKey)];
        //
//        NSIndexPath *indexPath = [self getAssociatedValueForKey:(__bridge void * _Nonnull)(currentSelectedKey)];
        menu.reOpenSelected = YES;
        menu.currentSelected = 0;
        menu.textAlignment = NSTextAlignmentCenter;
        menu.selectedColor = [UIColor blueColor];
    }
    [self dlpop_viewWillDisappear:animated];  // VC 将要消失
}


@end




















































